#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
int n = 4;
int data[4] = {5, 2, 1, 4};
void demo1(){   //STL
	sort(data, data + n);
	do{
		for(int i = 0; i < n; i++){
            cout<<data[i]<<" ";
        }
        cout<<endl;
	}while(next_permutation(data, data + 4));
}

// dfs
void swap(int index, int j){
    if(index == j) return;
    int temp = data[j];
    data[j] = data[index];
    data[index] = temp;
}

void dfs(int begin, int end){
    if(begin == end){
        for(int i = 0; i < end; i++){
            cout<<data[i]<<" ";
        }
        cout<<endl;
        return;
    }
    for(int j = begin; j < end; j++){
        swap(begin, j);
        dfs(begin + 1, end);
        swap(begin, j);
    }
}

void demo2(){
    sort(data, data + 4);
    dfs(0, n);
}



int main(){
    clock_t start, end;
    start = clock();
	demo1();
    end = clock();
    cout<<(double)(end - start)<<endl;
    cout<<"-----------------"<<endl;

    start = clock();
    demo2();
    end = clock();
    cout<<(double)(end - start)<<endl;
    return 0;
}